//
//  YGFoddItemDetailView.m
//  yuanshangyiliao
//
//  Created by icholab on 2021/4/26.
//

#import "YGFoddItemDetailView.h"
#import "YGFoodItemListTableViewCell.h"
#import <UIImageView+WebCache.h>
@interface YGFoddItemDetailView()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) UITableView  *tableView;
@property (nonatomic, strong) UIImageView  *coverImageView;
@property (nonatomic, strong) UIButton  *closeBtn;
@property (nonatomic, strong) UILabel  *titleLabel;
@property (nonatomic, strong) UIView  *contentView;
@property (nonatomic, strong) NSArray  *dataArray;

@end
@implementation YGFoddItemDetailView

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        
        [self initView];
        [self initMas];
    }
    return self;
}

- (void)initView{
    UIView *bgView = [[UIView alloc] initWithFrame:self.bounds];
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(clickCloseBtn)];
    [bgView addGestureRecognizer:tap];
    bgView.backgroundColor = UIColorFromRGBAlpha(0x030303, 0.3);
    [self addSubview:bgView];

    [bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.bottom.mas_equalTo(0);
    }];

    [self addSubview:self.contentView];
    [self.contentView addSubview:self.tableView];
    [self.contentView addSubview:self.coverImageView];
    [self.contentView addSubview:self.titleLabel];
    [self.contentView addSubview:self.closeBtn];
}

- (void)initMas{
    [self.contentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.mas_equalTo(self);
        make.width.mas_equalTo(310);
        make.height.mas_equalTo(385);
    }];
    
    [self.coverImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(10);
        make.height.width.mas_equalTo(100);
        make.centerX.mas_equalTo(self);
    }];

    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(self.contentView);
        make.centerX.mas_equalTo(self.contentView);
        make.height.mas_equalTo(14);
        make.top.mas_equalTo(self.coverImageView.mas_bottom).offset(10);
    }];

    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(250);
        make.height.mas_equalTo(200);
        make.top.mas_equalTo(self.titleLabel.mas_bottom).offset(18);
        make.centerX.mas_equalTo(self.contentView);
    }];
    
    [self.closeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.mas_equalTo(22);
        make.top.mas_equalTo(10);
        make.right.mas_equalTo(self.contentView.mas_right).offset(-10);
    }];
}

- (void)configureModel:(YGServerDetailModel *)model{
    [self.coverImageView sd_setImageWithURL:[NSURL URLWithString:model.thumbnail]];
    self.titleLabel.text = model.typeName;
    self.dataArray = model.researchDel;
    [self.tableView reloadData];
}

- (void)clickCloseBtn{
    self.hidden = YES;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    YGFoodItemListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"YGFoodItemListTableViewCell"];
    if (!cell) {
        cell = [[YGFoodItemListTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"YGFoodItemListTableViewCell"];
    }
    YGServerItemModel *model = [self.dataArray objectAtIndex:indexPath.row];
    [cell configureModel:model];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 33;
    
}
- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc] init];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.alwaysBounceVertical = NO;
        _tableView.alwaysBounceHorizontal = NO;
        _tableView.bounces = NO;
        _tableView.separatorStyle = UITableViewCellAccessoryNone;
        [_tableView registerClass:YGFoodItemListTableViewCell.class forCellReuseIdentifier:@"YGFoodItemListTableViewCell"];
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.showsHorizontalScrollIndicator = NO;

        _tableView.layer.borderWidth = 0.5;
        _tableView.clipsToBounds = YES;
        _tableView.layer.cornerRadius = 4;
        _tableView.layer.borderColor = UIColorFromRGB(0x37C8D7).CGColor;
    }
    return _tableView;
}

- (UIImageView *)coverImageView{
    if (!_coverImageView) {
        _coverImageView = [[UIImageView alloc] init];
    }
    return _coverImageView;
}
 
- (UILabel *)titleLabel{
    if (!_titleLabel) {
        _titleLabel = [[UILabel alloc] init];
        _titleLabel.textColor = UIColorFromRGB(0x333333);
        _titleLabel.font = [UIFont systemFontOfSize:12];
        _titleLabel.textAlignment = NSTextAlignmentCenter;
    }
    return _titleLabel;
}

- (UIButton *)closeBtn{
    if (!_closeBtn) {
        _closeBtn = [[UIButton alloc] init];
        [_closeBtn addTarget:self action:@selector(clickCloseBtn) forControlEvents:UIControlEventTouchUpInside];
        [_closeBtn setImage:[UIImage imageNamed:@"food_close"] forState:UIControlStateNormal];
    }
    return _closeBtn;
}

- (UIView *)contentView{
    if (!_contentView) {
        _contentView = [[UIView alloc] init];
        _contentView.backgroundColor = [UIColor whiteColor];
        _contentView.layer.cornerRadius = 8;
        _contentView.clipsToBounds = YES;
    }
    return _contentView;
}
@end
